package io.github.talelin.sleeve.core.config;

import io.github.talelin.sleeve.module.message.MessageWebSocketHandler;
import io.github.talelin.sleeve.module.message.WsHandler;
import io.github.talelin.sleeve.module.message.WsHandlerImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@ConditionalOnProperty(prefix = "bld.websocket", value = "enable", havingValue = "true")
@EnableWebSocket
public class WebsocketConfiguration implements WebSocketConfigurer {
    @Bean
    public MessageWebSocketHandler messageWebSocketHandler() {
        return new MessageWebSocketHandler();
    }
    @Bean
    public WsHandler wsHandler() {
        return new WsHandlerImpl();
    }
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(messageWebSocketHandler(), "ws/message")
                .setAllowedOrigins("*");
    }
}
